package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.Page;
import org.kjtc.entity.User;
import org.kjtc.entity.UserInfo;
import org.kjtc.entity.UserRole;
import org.kjtc.service.ActionHistoryService;
import org.kjtc.service.UserInfoService;
import org.kjtc.service.UserRoleService;
import org.kjtc.service.UserService;
import org.kjtc.util.Const;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserManagController extends BaseController{

	 @Resource(name = "userService")
	 private UserService userService;
	 @Resource(name = "userRoleService")
	 private UserRoleService userRoleService;
	 @Resource(name = "userInfoService")
	 private UserInfoService userInfoService;
	 @Resource(name = "actionHistoryService")
	 private ActionHistoryService actionHistoryService;

     @RequestMapping(value = "/searchUser")
     @ResponseBody
     public Map<String, Object>searchUser(UserInfo userInfo, String page, String rows) {

    	Map<String, Object> map = new HashMap<String, Object>();
    	logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户信息查询开始");
		// 分页
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));
		userInfo.setSessionRoleID(this.getUesrInfo().getRoleID());
		userInfo.setSessionUserID(this.getUesrInfo().getUserID());

		List <UserInfo> list = null;
		try {
			if (userInfo.getBranchCompanyID() == "") {
				userInfo.setBranchCompanyID(this.getBranchCompanyID());
			}
			list = userInfoService.getUserInfoPage(userInfo, pageQuery);
			map.put("totalCnt",  pageQuery.getTotalResult());
			map.put("totalPage", pageQuery.getTotalPage());
			map.put("userInfos", list);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户信息查询异常" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户信息查询结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "用户信息查询");
		userInfo = null;
		return map;
     }

    @RequestMapping(value="/saveUser")
    @ResponseBody
    @Transactional
 	public Map<String, Object> saveUser(User user, UserInfo userInfo, UserRole userRole) {

    	Map<String, Object> map = new HashMap<String, Object>();
    	logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户信息添加开始");
    	String userID = this.getUesr().getUserID();
    	user.setCreateUser(userID);
    	user.setUpdateUser(userID);
    	userInfo.setCreateUser(userID);
    	userInfo.setUpdateUser(userID);
    	userRole.setCreateUser(userID);
    	userRole.setUpdateUser(userID);

 		int count1 = 0;
 		int count2 = 0;
 		int count3 = 0;
 		try {
			count1 = userService.saveUser(user);
			count2 = userInfoService.saveUserInfo(userInfo);
			count3 = userRoleService.saveUserRole(userRole);

			if (count1 > 0 && count2 > 0 && count3 > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "添加用户" + userInfo.getUserName() + "(" + userInfo.getUserID() + ")");
 		} catch (DuplicateKeyException ex){
			map.put("result", "DuplicateKeyException");
 		} catch (Exception e) {
 			e.printStackTrace();
 			map.put("result", "Error");
 		    logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户信息添加异常" + e.getMessage());
 		}
 		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户信息添加结束");
 		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "用户信息添加");
 		user = null;
 		userInfo = null;
 		userRole = null;
		return map;
 	}

 	@RequestMapping(value="/updateUser")
 	@ResponseBody
 	@Transactional
 	public Map<String,Object> updateUser(User user, UserInfo userInfo, UserRole userRole){

 		Map<String, Object> map = new HashMap<String, Object>();
 		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户信息修改开始");
    	String userID = this.getUesr().getUserID();
    	userInfo.setUpdateUser(userID);
    	userRole.setUpdateUser(userID);
    	int count1 = 0;
 		int count2 = 0;
 		int count3 = 0;
 		try {
 			count1 = userService.updateUser(user);
 			count2 = userInfoService.updateUserInfo(userInfo);
 			count3 = userRoleService.updateUserRole(userRole);
			if (count1 == 0 && count2 == 0 && count3 == 0) {
				map.put("result", "NotFound");
			} else {
				map.put("result", "Success");
				List<UserInfo> userInfoList = userInfoService.getUserInfo(this.getUesrInfo());
				if (userInfoList.size() > 0) {
					this.getSession().setAttribute(Const.SESSION_USERINFO, userInfoList.get(0));
				}
				map.put("userName", this.getUesrInfo().getUserName());
			}
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "修改用户" + userInfo.getUserName());
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户信息修改异常" + e.getMessage());
		}
 		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户信息修改结束");
 		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "用户信息修改");
 		userInfo = null;
 		userRole = null;
		return map;
 	}

 	@RequestMapping(value="/updateUserInfo")
 	@ResponseBody
 	@Transactional
 	public Map<String,Object> updateUserInfo(UserInfo userInfo){

 		Map<String, Object> map = new HashMap<String, Object>();
 		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户个人信息修改开始");
 		String userID = this.getUesr().getUserID();
 		userInfo.setUpdateUser(userID);
 		userInfo.setEnabled(true);
 		int count = 0;
 		try {
 			count = userInfoService.updateUserInfo(userInfo);
 			if (count == 0) {
 				map.put("result", "NotFound");
 			} else {
 				map.put("result", "Success");
 				List<UserInfo> userInfoList = userInfoService.getUserInfo(this.getUesrInfo());
 				if (userInfoList.size() > 0) {
 					this.getSession().setAttribute(Const.SESSION_USERINFO, userInfoList.get(0));
 				}
 				map.put("userName", this.getUesrInfo().getUserName());
 			}
 		} catch (Exception e) {
 			e.printStackTrace();
 			map.put("result", "Error");
 			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户个人信息修改异常" + e.getMessage());
 		}
 		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户个人信息修改结束");
 		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "用户个人信息修改");
 		userInfo = null;
 		return map;
 	}

 	@RequestMapping(value = "/deleteUser")
 	@ResponseBody
 	@Transactional
 	public Map<String, Object> deleteUser(String userID) {

 		Map<String, Object> map = new HashMap<String, Object>();
 		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户信息删除开始");
 		String [] userIDList = userID.split(",");
 		User user = new User();
 		user.setUserIDList(userIDList);
 		user.setUserID(this.getUesr().getUserID());
        UserInfo userInfo = new UserInfo();
        userInfo.setUserIDList(userIDList);
        userInfo.setUserID(this.getUesr().getUserID());
        UserRole userRole = new UserRole();
 		userRole.setUserIDList(userIDList);
 		userRole.setUserID(this.getUesr().getUserID());
 		int count1 = 0;
 		int count2 = 0;
 		int count3 = 0;
 		try {
 			count1 = userService.deleteUser(user);
 			count2 = userInfoService.deleteUserInfo(userInfo);
 			count3 = userRoleService.deleteUserRole(userRole);
			if (count1 == 0 && count2 == 0 && count3 == 0) {
				map.put("result", "NotFound");
			} else {
				map.put("result", "Success");
			}
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "删除用户" + userInfo.getUserName() + "(" + userInfo.getUserID() + ")");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户信息删除异常" + e.getMessage());
		}

 		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户信息删除结束");
 		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "用户信息删除");
 		user = null;
 		userInfo = null;
 		userRole = null;
 		return map;
 	}

 	@RequestMapping(value="/updatePassword")
 	@ResponseBody
 	public Map<String, Object> updatePassword(User user){

 		Map<String, Object> map = new HashMap<String, Object>();
 		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "密码修改开始");
 		user.setUpdateUser(this.getUesr().getUserID());
 		int count = 0;
 		try {
			count = userService.updatePassword(user);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "修改用户" + "(" + user.getUserID() + ")密码");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "密码修改异常" + e.getMessage());
		}
 		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "密码修改结束");
 		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "密码修改");
 		user = null;
		return map;
 	}

 	@RequestMapping(value="/updatePasswordInit")
 	@ResponseBody
 	public Map<String, Object> updatePasswordInit(User user){

 		Map<String, Object> map = new HashMap<String, Object>();
 		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "强制修改密码开始");
 		int count = 0;
 		try {
			count = userService.updatePassword(user);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "强制修改用户" + "(" + user.getUserID() + ")密码");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "强制修改密码异常" + e.getMessage());
		}
 		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "强制修改密码结束");
 		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "强制修改密码");
 		user = null;
		return map;
 	}

 	@RequestMapping(value = "/searchUserInfo")
    @ResponseBody
    public Map<String, Object>searchUser() {

 		Map<String, Object> map = new HashMap<String, Object>();
    	logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户个人信息查询开始");
		map.put("userInfo", this.getUesrInfo());
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户个人信息查询结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "用户个人信息查询");
		return map;
     }
}
